package com.maybach.workflow.core.processor.impl;


import com.maybach.workflow.common.api.WorkFlowContext;
import com.maybach.workflow.common.api.node.BaseNode;
import com.maybach.workflow.common.api.node.ForkNode;
import com.maybach.workflow.common.model.FlowTaskDO;
import com.maybach.workflow.core.processor.ProcessorFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by houenxun on 15/8/13.
 */
public class ForkProcessor extends AbstractProcessor{
    @Override
    public boolean runIn(WorkFlowContext workFlowContext) {
        this.runOut(workFlowContext);
        return true;
    }

    @Override
    public List<BaseNode> getNextNodes(WorkFlowContext workFlowContext) {
        List<BaseNode> list = new ArrayList<BaseNode>();
        ForkNode forkNode = (ForkNode)workFlowContext.getThisNode();
        FlowTaskDO lastTask = workFlowContext.getThisTask() == null ? workFlowContext.getLastTask() : workFlowContext.getThisTask();
        List<BaseNode> forkNodeList = forkNode.getForkNodeList();
        for (BaseNode child : forkNodeList) {
            workFlowContext.setThisNode(child);
            workFlowContext.setLastTask(lastTask);
            list.addAll(super.getCommonNextNodes(workFlowContext));
        }
        return list;
    }

    @Override
    public void runOut(WorkFlowContext workFlowContext) {
        ForkNode forkNode = (ForkNode)workFlowContext.getThisNode();
        FlowTaskDO lastTask = workFlowContext.getThisTask() == null ? workFlowContext.getLastTask() : workFlowContext.getThisTask();
        List<BaseNode> forkNodeList = forkNode.getForkNodeList();
        for (BaseNode child : forkNodeList) {
            workFlowContext.setThisNode(child);
            workFlowContext.setLastTask(lastTask);
            ProcessorFactory.getProcessor(child.getNodeType()).runIn(workFlowContext);
        }
    }


}
